﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Von.Library;
using Von.Web.Framework;
using Von.Web.Interfaces;
using Von.Web.Models;
using Von.Web.Structs;
using Von.Web.Tasks;

namespace Von.Web.Controllers
{
    public class AdminController : WebControllerBase
    {
        #region "inherited properties"
        private IEmailSender emailSender = null;

        public override ApplicationDefinationBase App => Statement.App;

        /// <summary>
        /// 构造函数，实例化了日志工具
        /// </summary>
        /// <param name="logger">日志实例</param>
        public AdminController(IEmailSender EmailSender)
        {
            emailSender = EmailSender;
        }
        public IActionResult Index()
        {
            if (!IsLogin) return Redirect("/Account/Login");
            ViewBag.DisplaySpliter = true;
            Dictionary<string, List<MyTaskInfo>> list = Runtime.GetMyTask(UserInfo.ID);
            return View(list);
        }
        #endregion
        #region "ApplicationManage"
        [Action("8de08371-00b5-475f-935e-45526404391f", "应用管理", "应用管理", EControlKind.CK_View)]
        public async Task<IActionResult> ApplicationManage()
        {
            return View(Runtime.ListApplication());
        }
        /// <summary>
        /// 根据主键 PK_Module 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("95f29b93-9e00-46ec-b93e-3bcf65b2529f", "应用管理", "获取应用信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> GetApplication(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(param.Json["ID"].ToString());
            var info = Runtime.GetApplication(ID);
            return ExecuteResult.Success(info);
        }
        [Action("a11aa9a9-9d77-42a1-b38b-012188c81ca2", "应用管理", "保存应用信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveApplication(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "")
                return Failed("0003", "参数错误系统拒绝执行！"); ;
            Guid ID = Guid.Parse(param.Json["ID"].ToString());
            ApplicationDefinationBase app = Runtime.GetApplication(ID);
            app.ID = ID;
            app.AppName = param.Json.Value<string>("AppName");
            app.AppTitle = param.Json.Value<string>("AppTitle");
            app.Prefix = param.Json.Value<string>("Prefix");
            app.Owner = param.Json.Value<string>("Owner");
            app.Qualifier = param.Json.Value<string>("Qualifier");
            app.AssemlyName = param.Json.Value<string>("AssemlyName");
            app.Cert = param.Json.Value<string>("Cert");
            app.AppSetting = JObject.Parse(param.Json.Value<string>("AppSetting"));
            Runtime.UpdateApplication(app);
            return ExecuteResult.Success(app);
        }
        #endregion
        #region "TicketNoManage"
        [Action("19140571-164c-4021-97d6-9e0b1703aba2", "票证编码管理", "票证编码管理界面", EControlKind.CK_View)]
        public async Task<IActionResult> TicketNoManage()
        {
            TicketNoTask task = new TicketNoTask();
            List<TicketNoInfo> list = await task.List<TicketNoInfo>();
            return View(list);
        }
        /// <summary>
        /// 获取单据号
        /// </summary>
        [Action("{2373FDE3-76ED-4F95-AA3F-FA56D7CB6439}", "票证编码管理", "获取单据号", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> GetTicketNo(JsonStr param)
        {
            return ExecuteResult.Success(await TicketNoTask.GetTicketNo(param.Json["RuleName"].ToString()));
        }
        /// <summary>
        /// 测试单据号
        /// </summary>
        [Action("{A73E2AC9-BCD7-47BF-ADD2-8782EB0F7726}", "票证编码管理", "测试单据号", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> TestTicketNo(JsonStr param)
        {
            if (param.Json["Rule"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            TicketNoInfo info = new TicketNoInfo();
            info.RuleName = param.Json["RuleName"].ToString();
            info.CurrentID = param.Json.Value<int>("CurrentID");
            info.Kind = param.Json.Value<int>("Kind");
            info.NumLength = param.Json.Value<int>("NumLength");
            info.Rule = param.Json["Rule"].ToString();
            info.LastTime = DateTime.Now;
            return ExecuteResult.Success(TicketNoTask.GetTicketNo(info, DateTime.Now));
        }
        /// <summary>
        /// 根据主键 PK_TicketNo 提取信息
        /// </summary>
        /// <param name="RuleName">规则名称</param>
        [Action("4191027f-3ca2-4c4f-a584-0cc1a74d3e99", "票证编码管理", "获取票证编码规则信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> GetByTicketNo(JsonStr param)
        {
            if (param.Json["RuleName"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            string RuleName = param.Json["RuleName"].ToString();
            TicketNoTask task = new TicketNoTask();
            TicketNoInfo info = await task.GetByTicketNo<TicketNoInfo>(RuleName);
            return ExecuteResult.Success(info);
        }
        [Action("443789b5-e301-4931-87b9-4b35c2603c8d", "票证编码管理", "保存票证编码规则信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveTicketNo(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "") return await AddTicketNo(param);
            else return await EditTicketNo(param);
        }
        [Action("32b8d6f0-7d61-4931-9a22-337a4dac87ab", "票证编码管理", "添加票证编码规则信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> AddTicketNo(JsonStr param)
        {
            TicketNoInfo info = new TicketNoInfo();
            info.RuleName = param.Json["RuleName"].ToString();
            info.LastTime = DateTime.Now;
            info.CurrentID = param.Json.Value<int>("CurrentID");
            info.Kind = param.Json.Value<int>("Kind");
            info.NumLength = param.Json.Value<int>("NumLength");
            info.Rule = param.Json["Rule"].ToString();
            TicketNoTask task = new TicketNoTask();
            if (!await task.Add(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("9204df67-8942-4134-af51-6aea2038f6b5", "票证编码管理", "修改票证编码规则信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> EditTicketNo(JsonStr param)
        {
            TicketNoInfo info = new TicketNoInfo();
            info.RuleName = param.Json["RuleName"].ToString();
            info.LastTime = DateTime.Now;
            info.CurrentID = param.Json.Value<int>("CurrentID");
            info.Kind = param.Json.Value<int>("Kind");
            info.NumLength = param.Json.Value<int>("NumLength");
            info.Rule = param.Json["Rule"].ToString();
            TicketNoTask task = new TicketNoTask();
            if (!await task.Edit(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("099bdb85-0ffd-4449-82bc-27c63d916d45", "票证编码管理", "删除票证编码规则信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> DelTicketNo(JsonStr param)
        {
            if (param.Json["RuleName"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            string RuleName = param.Json["RuleName"].ToString();
            TicketNoTask task = new TicketNoTask();
            if (await task.Del(RuleName)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        #endregion
        #region "Menu manage"
        [Authorize]
        [Action("{1EFA6923-BAD0-40BE-8BB3-E6919A48FA82}", "菜单管理", "菜单管理界面", EControlKind.CK_View)]
        public async Task<ActionResult> MenuManage()
        {
            return View();
        }
        private async Task<List<MenuViewModel>> loadChildrenMenu(MenuTask task, Guid ID, bool hasChildren, string roleName = "")
        {
            List<MenuViewModel> menus = string.IsNullOrEmpty(roleName) ? await task.ListByMenu_PID<MenuViewModel>(ID) : await task.ListByMenuWithRight(ID, roleName);  //原有功能
            if (hasChildren)
                foreach (MenuViewModel m in menus)
                    m.children = await loadChildrenMenu(task, m.ID, true, roleName);
            return menus;
        }

        [Action("{B4C9455A-E91A-4575-B476-853822BDC01B}", "菜单管理", "获取菜单类型", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> LoadMenuRoot()
        {
            MenuTask task = new MenuTask();
            List<MenuViewModel> list = await loadChildrenMenu(task, Guid.Empty, false);
            return ExecuteResult.Success(list);
        }
        [Action("{92B01F4C-DC3A-45C7-BA0D-892FC23DEC0D}", "菜单管理", "提取菜单信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> LoadMenu(JsonStr Param)
        {
            if (Param.Json["ID"] == null) return Failed("0105", "执行参数异常");
            MenuTask task = new MenuTask();
            List<MenuViewModel> list = await loadChildrenMenu(task, Guid.Parse(Param.Json["ID"].ToString()), true);
            return ExecuteResult.Success(list);
        }
        private MenuInfo JsonToMenuInfo(JObject Data)
        {
            MenuInfo info = new MenuInfo();
            info.MenuName = Data["MenuName"] != null ? Data["MenuName"].ToString() : "";
            info.MenuClass = Data["MenuClass"] != null ? Data["MenuClass"].ToString() : "";
            info.iconFilename = Data["iconFilename"] != null ? Data["iconFilename"].ToString() : "";
            info.MenuData = Data["MenuData"] != null ? Data["MenuData"].ToString() : "";
            info.PID = (Data["PID"] == null || string.IsNullOrEmpty(Data["PID"].ToString())) ? Guid.Empty : Guid.Parse(Data["PID"].ToString());
            info.DisplayOrder = Data["DisplayOrder"] != null ? Data.Value<int>("DisplayOrder") : 1;
            return info;
        }
        private async Task<Guid> AddMenuAsync(JsonStr Param)
        {
            MenuInfo info = JsonToMenuInfo(Param.Json as JObject);
            info.ID = Guid.NewGuid();
            MenuTask task = new MenuTask();
            if (!await task.Add(info)) return Guid.Empty;
            return info.ID;
        }
        private async Task<bool> EditMenuAsync(JsonStr Param)
        {
            MenuInfo info = JsonToMenuInfo(Param.Json as JObject);
            info.ID = Guid.Parse(Param.Json["ID"].ToString());
            MenuTask task = new MenuTask();
            return await task.Edit(info);
        }
        [Action("{AAEDEF18-228E-4800-BA15-2AC19CDCBAA7}", "菜单管理", "存储菜单信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveMenu(JsonStr Param)
        {
            if (Param.Json["ID"] == null || string.IsNullOrEmpty(Param.Json["ID"].ToString()))
            {
                Guid result = await AddMenuAsync(Param);
                if (result == Guid.Empty) return Failed("0105", "执行参数异常，无法执行");
                else return ExecuteResult.Success(result);
            }
            else if (await EditMenuAsync(Param)) return ExecuteResult.Success(true);
            else return Failed("0105", "执行参数异常，无法执行");
        }
        [Action("{DB96FB07-A046-4F64-B857-9D416E90F949}", "菜单管理", "调整菜单顺序", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> MoveMenu(JsonStr Param)
        {
            if (Param.Json["ID"] == null || string.IsNullOrEmpty(Param.Json["ID"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            if (Param.Json["Order"] == null || string.IsNullOrEmpty(Param.Json["Order"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            MenuTask task = new MenuTask();
            return ExecuteResult.Success(await task.Move(Guid.Parse(Param.Json["ID"].ToString()), Param.Json.Value<int>("Order")));
        }
        [Action("{EB21588C-AD8A-4C25-865C-DFBD0385BC8B}", "菜单管理", "删除菜单信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> DeleteMenu(JsonStr Param)
        {
            if (Param.Json["ID"] == null || string.IsNullOrEmpty(Param.Json["ID"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            MenuTask task = new MenuTask();
            if (await task.Del(Guid.Parse(Param.Json["ID"].ToString()))) return ExecuteResult.Success(true);
            else return Failed("0105", "执行参数异常，无法执行");
        }
        #endregion
        #region "User Manage"
        [Authorize]
        [Action("2dd1a7f5-183d-4e11-9a84-a179a230e180", "登录人员管理", "登录人员管理界面", EControlKind.CK_View)]
        public async Task<IActionResult> UserManage()
        {
            ViewBag.PWDRegex = App.AppSetting["PasswordStrength"] == null
                    ? @"(?=.*[0-9])(?=.*[a-zA-Z])(?=([\x21-\x7e]+)[^a-zA-Z0-9]).{8,30}"
                    : App.AppSetting["PasswordStrength"].ToString();
            ViewBag.PWDRequirement = App.AppSetting["PasswordRequirement"] == null
                    ? @"密码必须包含数字、小写或大写字母、特殊字符、字符数在8-30之间。"
                    : App.AppSetting["PasswordRequirement"].ToString();
            IdentityUserTask task = new IdentityUserTask();
            List<IdentityUserInfo> list = await task.List<IdentityUserInfo>();
            return View(list);
        }
        [Authorize]
        [Action("1b45987b-800d-49ca-ab92-e1f32273666f", "登录人员管理", "获取登录人员信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> GetByIdentity(JsonStr Param)
        {
            if (Param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(Param.Json["ID"].ToString());
            IdentityUserTask task = new IdentityUserTask();
            IdentityUserInfo info = await task.GetByIdentity(ID);
            List<SubsidiaryInfo> subs = await info.GetSubsidiaries();
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("ID", info.ID);
            result.Add("LoginName", info.LoginName);
            result.Add("Email", info.Email);
            result.Add("PhoneNumber", info.PhoneNumber);
            result.Add("DisplayName", info.DisplayName);
            result.Add("LockoutEnabled", info.LockoutEnabled);
            result.Add("LockoutEnd", info.LockoutEnd);
            result.Add("Status", info.Status);
            result.Add("SysFlag", (int)info.SysFlag);
            foreach(var item in subs)
            {
                result.Add(item.SubName, item.SubValue);
            }
            return ExecuteResult.Success(result);
        }
        [Authorize]
        [Action("9e8e99ea-d1a3-42df-a705-7a090c231e08", "登录人员管理", "修改登录人员信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> EditIdentity(JsonStr param)
        {
            IdentityUserTask task = new IdentityUserTask();
            Guid ID = Guid.Parse(param.Json.Value<string>("ID"));
            IdentityUserInfo info = await task.GetByIdentity(ID);
            if (info == null) return Failed("0105", "");
            List<SubsidiaryInfo> subs = await info.GetSubsidiaries();
            foreach (var p in param.Json)
            {
                if (p.Key == "ID" || p.Key == "LoginName" || p.Key == "InputPassword" || p.Key == "ConfirmPassword") { }
                else if (p.Key == "Email")
                {
                    if (!info.Email.Equals(param.Json.Value<string>("Email"), StringComparison.OrdinalIgnoreCase))
                    {
                        if (!await task.CheckDoubly("Email", param.Json.Value<string>("Email")))
                            return Failed("1003", "邮箱");
                        info.Email = param.Json.Value<string>("Email");
                        info.EmailConfirmed = false;
                    }
                }
                else if (p.Key == "PhoneNumber")
                {
                    if (!info.PhoneNumber.Equals(param.Json.Value<string>("PhoneNumber"), StringComparison.OrdinalIgnoreCase))
                    {
                        if (!await task.CheckDoubly("PhoneNumber", param.Json.Value<string>("PhoneNumber")))
                            return Failed("1003", "电话号码");
                        info.PhoneNumber = param.Json.Value<string>("PhoneNumber");
                        info.PhoneNumberConfirmed = false;
                    }
                }
                else if (p.Key == "SysFlag")
                {
                    if (param.Json["SysFlag"] != null && info.SysFlag != (IIdentity_Sys_Flag)param.Json.Value<int>("SysFlag"))
                    {
                        info.SysFlag = (IIdentity_Sys_Flag)param.Json.Value<int>("SysFlag");
                    }
                }
                else if (p.Key == "DisplayName")
                {
                    if (!info.DisplayName.Equals(param.Json.Value<string>("DisplayName"), StringComparison.OrdinalIgnoreCase))
                    {
                        info.DisplayName = param.Json.Value<string>("DisplayName");
                    }
                }
                else
                {
                    SubsidiaryInfo subInfo = subs.FirstOrDefault(s => s.SubName == p.Key);
                    if (subInfo == null) subs.Add(new SubsidiaryInfo(0, ID, p.Key, p.Value.ToString()));
                    else subInfo.SubValue = p.Value.ToString();
                }
            }
            await task.Edit(info);
            return ExecuteResult.Success(info);
        }
        [Authorize]
        [Action("aae468fc-5c93-4a11-a290-a9e05255b713", "登录人员管理", "删除登录人员信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> DeleteIdentity(JsonStr Param)
        {
            if (Param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(Param.Json["ID"].ToString());
            DbTransaction trans = (DbTransaction)Statement.Accession.BeginDBTrans();
            try
            {
                IdentityUserTask task = new IdentityUserTask(trans);
                UserRoleTask roleTask = new UserRoleTask(trans);
                if (!await roleTask.DelUser(ID))
                {
                    trans.Rollback();
                    return ExecuteResult.Failed(App.ResxName, "USER001", ID.ToString());
                }
                if (!await task.Del(ID))
                {
                    trans.Rollback();
                    return ExecuteResult.Failed(App.ResxName, "USER002", ID.ToString());
                }
                trans.Commit();
                return ExecuteResult.Success(true);
            }
            catch (Exception e)
            {
                trans.Rollback();
                Log.ErrorFormat("DeleteIdentity %s raise %s", ID.ToString(), e.Message);
            }
            return Failed("9999", "删除失败！");
        }
        [Authorize]
        [Action("{A4E1AF58-479E-47D3-8575-52237DF8D683}", "登录人员管理", "解锁登录人员", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> UnlockIdentity(JsonStr Param)
        {
            if (Param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(Param.Json["ID"].ToString());
            IdentityUserTask task = new IdentityUserTask();
            return ExecuteResult.Success(await task.LoginSuccessed(ID));
        }
        [Action("{179E6CB5-B6F5-43C9-90C8-F7E3DF6263DB}", "登录人员管理", "提取人员扩展信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> ListSubsidiary(JsonStr param)
        {
            SubsidiaryTask task = new SubsidiaryTask();
            List<SubsidiaryInfo> list = await task.ListBySUBSIDIARY_REF_IDENTITY<SubsidiaryInfo>(Guid.Parse(param.Json["ID"].ToString()));
            return ExecuteResult.Success(list);
        }
        [Authorize]
        [Action("{2E2EC883-C7F7-400F-B352-487E3BB6214F}", "登录人员管理", "保存人员扩展信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveSubsidiary(JsonStr param)
        {
            SubsidiaryTask task = new SubsidiaryTask();
            SubsidiaryInfo info = await task.GetBySubsidiary<SubsidiaryInfo>(
                Guid.Parse(param.Json["UserIdx"].ToString()),
                param.Json["Key"].ToString());
            if (info == null)
            {
                info = new SubsidiaryInfo(0, Guid.Parse(param.Json["UserIdx"].ToString()),
                    param.Json["Key"].ToString(), param.Json["Val"].ToString());
                await task.Add<SubsidiaryInfo>(info);
            } else {
                info.SubName = param.Json["Key"].ToString();
                info.SubValue = param.Json["Val"].ToString();
                await task.Edit<SubsidiaryInfo>(info);
            }
            return ExecuteResult.Success(info);
        }
        [Authorize]
        [Action("{6C4653EF-7256-455E-A743-666E816D7E8C}", "登录人员管理", "删除人员扩展信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> DeleteSubsidiary(JsonStr param)
        {
            SubsidiaryTask task = new SubsidiaryTask();
            return ExecuteResult.Success(await task.Del(int.Parse(param.Json["ID"].ToString())));
        }
        [Authorize]
        [Action("{40A1AE42-4AAF-4EEC-B751-950456631C8B}", "登录人员管理", "重置人员登录密码", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> ResetPassword(JsonStr Param)
        {
            Log.Debug("ResetPassword");
            IdentityUserTask task = new IdentityUserTask();
            ExecuteResult result = await task.CheckPassword(Param.Json["Password"].ToString());
            if (!result.Successed) return result;
            if (await task.ChangePassword(Guid.Parse(Param.Json["UserID"].ToString()), Param.Json["Password"].ToString()))
                return ExecuteResult.Success(true);
            return ExecuteResult.Failed(App.ResxName, "0107");
        }
        #endregion
        #region "Role Manage"
        [Authorize]
        [Action("{0A382D55-1D61-42E2-B944-8ECA72EE4AFD}", "角色管理", "角色管理界面", EControlKind.CK_View)]
        public async Task<ActionResult> RoleManage()
        {
            IdentityRoleTask task = new IdentityRoleTask();
            List<IdentityRoleInfo> list = await task.List<IdentityRoleInfo>();
            return View(list);
        }
        [Authorize]
        [Action("{66150750-3E12-402F-A9E9-1DD4DAA86D19}", "角色管理", "提取角色信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> GetRole(JsonStr Param)
        {
            if (Param.Json["RoleName"] == null || string.IsNullOrEmpty(Param.Json["RoleName"].ToString()))
            {
                return Failed("0012", "未发现对应的模块。");
            }
            IdentityRoleTask task = new IdentityRoleTask();
            IdentityRoleInfo info = await task.GetByRole(Param.Json["RoleName"].ToString());
            return ExecuteResult.Success(info);
        }
        [Authorize]
        [Action("{CDD00E80-4E0E-4D4C-955F-1AA8D78656E2}", "角色管理", "保存角色信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveRole(JsonStr Param)
        {
            IdentityRoleTask task = new IdentityRoleTask();
            IdentityRoleInfo info = new IdentityRoleInfo()
            {
                RoleName = Param.Json["RoleName"].ToString(),
                RoleGroup = Param.Json["RoleGroup"].ToString(),
                Note = Param.Json["Note"].ToString()
            };
            bool isNew = Param.Json.Value<bool>("isNew");
            ExecuteResult result = await task.Check(info);
            if (!result.Successed) return result;
            if (isNew && await task.GetByRole(info.RoleName) != null) return Failed("1003", "角色");
            if (isNew) await task.Add(info); else await task.Edit(info);
            return ExecuteResult.Success(info);
        }
        [Authorize]
        [Action("{5FECBF1A-B5FB-407B-8C5B-AC00B42CDAAB}", "角色管理", "删除角色信息", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> DeleteRole(JsonStr Param)
        {
            if (Param.Json["RoleName"] == null || string.IsNullOrEmpty(Param.Json["RoleName"].ToString()))
            {
                return Failed("0012", "未发现对应的模块。");
            }
            IdentityRoleTask task = new IdentityRoleTask();
            if (await task.Del(Param.Json["RoleName"].ToString())) return ExecuteResult.Success(true);
            return Failed("0107", "");
        }
        #endregion
        #region "Role right"
        [Authorize]
        [Action("{92EA5E75-9812-44FC-8DF6-E587550CA63E}", "角色权限管理", "角色权限管理界面", EControlKind.CK_View)]
        public async Task<ActionResult> RoleRightManage(string role)
        {
            if (string.IsNullOrEmpty(role)) throw new ApplicationException();
            RightTask task = new RightTask();
            List<RightInfo> lst = await task.ListByRIGHT_REF_ROLE<RightInfo>(role);
            RoleRightViewModel R = new RoleRightViewModel();
            R.RoleName = role;
            foreach (var app in Runtime.ListApplication())
            {
                RoleApplicationRightViewModel M = null;
                foreach (var action in app.Actions)
                {
                    if (M == null || M.ModuleName != action.ModuleName)
                    {
                        if (M != null && M.Actions.Count > 0) R.AppRights.Add(M);
                        M = new RoleApplicationRightViewModel();
                        M.AppID = app.ID;
                        M.AppName = app.AppName;
                        M.ModuleName = action.ModuleName;
                    }
                    if (action.ControlKind == EControlKind.CK_None) continue;
                    RoleActionRightViewModel A = new RoleActionRightViewModel();
                    A.ActionID = action.ID;
                    A.ActionName = action.Description;
                    A.Kind = (int)action.ControlKind;
                    A.Checked = lst.FirstOrDefault(r => r.DestIdx == action.ID) != null;
                    M.Actions.Add(A);
                }
                if (M != null && M.Actions.Count > 0) R.AppRights.Add(M);
            }
            MenuTask menuTask = new MenuTask();
            R.Menus = await loadChildrenMenu(menuTask, new Guid("61EF0AD7-CDA7-4810-A3FF-E964E046BFFD"), true, role);
            return View(R);
        }
        [Action("{B6E28E8E-C4E9-4BC8-A065-963933A66B48}", "角色权限管理", "角色权限存储", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveRight(JsonStr Param)
        {
            string Rolename = Param.Json["Rolename"].ToString();
            if (string.IsNullOrEmpty(Rolename))
                return Failed("0012", "未能发现提取信息。");
            //获取角色原有功能信息，循环原拥有的功能，检查是否有去除的，如果有去除的则进行删除操作
            RightTask task = new RightTask();
            List<RightInfo> rights = await task.ListByRIGHT_REF_ROLE<RightInfo>(Rolename);  //原有功能
            JArray checkedRights = (JArray)Param.Json["Rights"];  //现有功能
            var lstToDel = rights.Where(r => checkedRights.FirstOrDefault(c => c["actionID"].ToString() == r.DestIdx.ToString()) == null);
            foreach (RightInfo r in lstToDel)
            {
                if (!await task.Del(r.ID)) return Failed("0012", "删除失败！");
            }
            var lstToAdd = checkedRights.Where(c => rights.FirstOrDefault(r => c["actionID"].ToString() == r.DestIdx.ToString()) == null);
            foreach (JToken r in lstToAdd)
            {
                if (!await task.Add(new RightInfo(0, Rolename, Guid.Parse(r["actionID"].ToString()), r.Value<int>("kind"))))
                    return Failed("0012", "添加失败！");
            }
            return ExecuteResult.Success(true);
        }
        public async Task<ActionResult> MenuRightManage(string Role)
        {
            ViewData["Role"] = Role;
            return View();
        }
        #endregion
        #region "Role User"
        [Action("{12C76512-35B4-42C4-87B6-1C8F579F77DD}", "人员角色设置", "人员角色设置界面", EControlKind.CK_View)]
        public async Task<ActionResult> UserRoleManage(string id)
        {
            if (string.IsNullOrEmpty(id))
                throw new ApplicationException();
            IdentityUserTask userTask = new IdentityUserTask();
            IdentityUserInfo info = await userTask.GetByIdentity(Guid.Parse(id));
            if (info == null)
                throw new ApplicationException("您选择的用户并不存在");
            UserRoleTask userRoleTask = new UserRoleTask();
            IdentityRoleTask roleTask = new IdentityRoleTask();
            List<UserRoleInfo> userRoles = await userRoleTask.ListByUser<UserRoleInfo>(info.ID);
            List<IdentityRoleInfo> roles = await roleTask.List<IdentityRoleInfo>();
            List<RightViewModel> lst = new List<RightViewModel>();
            foreach (IdentityRoleInfo role in roles)
            {
                UserRoleInfo a = userRoles.FirstOrDefault(r => r.RoleName == role.RoleName);
                lst.Add(new RightViewModel() { RoleName = role.RoleName, Checked = a != null });
            }
            ViewBag.User = info;
            return View(lst);
        }
        [Action("{67D147BB-4B37-469A-BCB2-D4B5B82CDC25}", "人员角色设置", "人员角色存储", EControlKind.CK_Ajax)]
        public async Task<ExecuteResult> SaveUserRole(JsonStr Param)
        {
            if (Param.Json["ID"] == null)
                return Failed("0012", "未能发现提取信息。");
            Guid ID = Guid.Parse(Param.Json["ID"].ToString());
            JArray checkedRoles = (JArray)Param.Json["Roles"];    //新设定的用户 [RoleName,...]
            if (checkedRoles == null)
                return Failed("0012", "未能发现提取信息。");
            IdentityUserTask userTask = new IdentityUserTask();
            IdentityUserInfo user = await userTask.GetByIdentity(ID);
            if (user == null)
                return Failed("0012", "未能发现提取信息。");
            //获取用户原有角色信息，循环原拥有的角色，检查是否有去除的，如果有去除的则进行删除操作
            UserRoleTask userRoleTask = new UserRoleTask();
            List<UserRoleInfo> roles = await userRoleTask.ListByUser<UserRoleInfo>(ID);//原有角色
            var lstToDel = roles.Where(r => checkedRoles.FirstOrDefault(c => c.ToString() == r.ToString()) == null);    //失去的角色
            foreach (UserRoleInfo r in lstToDel)
            {
                if (!await userRoleTask.Del(r.ID)) return Failed("0107", "删除用户角色识别");
            }
            var lstToAdd = checkedRoles.Where(c => roles.FirstOrDefault(r => c.ToString() == r.ToString()) == null);    //新拥有的橘色
            foreach (JToken r in lstToAdd)
            {
                if (!await userRoleTask.Add(new UserRoleInfo(0, ID, r.ToString()))) return Failed("0107", "添加用户角色识别");
            }
            return ExecuteResult.Success(true);
        }
        #endregion
        #region "OptionManage"
        [Action("2ba2e43b-61c3-47b2-8537-4f72d22544db", "系统选项", "系统选项管理界面", EControlKind.CK_View)]
        [Authorize]
        public async Task<IActionResult> OptionManage()
        {
            OptionTask task = new OptionTask();
            List<OptionInfo> list = await task.List<OptionInfo>();
            return View(list);
        }

        /// <summary>
        /// 根据主键 PK_OPTION 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("83a76153-784e-45b8-ad0c-edbc55b29e7c", "系统选项", "获取系统选项信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetByOPTION(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            OptionTask task = new OptionTask();
            OptionModel info = await task.GetByOPTION<OptionModel>(param.Json["ID"].ToString());
            info.OptValues = await task.GetOptValues(info.OptName);
            return ExecuteResult.Success(info);
        }
        [Action("6ae2422e-f277-40ab-85fe-99abb5919882", "系统选项", "保存系统选项信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveOption(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "") return await AddOption(param);
            else return await EditOption(param);
        }
        private void dataToOption(JsonStr param, OptionModel info)
        {
            info.OptName = param.Json["OptName"].ToString();
            info.OptKind = param.Json.Value<int>("OptKind");
            info.OptInfo = param.Json["OptInfo"].ToString();
            info.OptValues = param.Json["OptValues"].ToString();
        }
        [Action("de1c1df8-c152-4148-8f06-548ab17953b7", "系统选项", "添加系统选项信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> AddOption(JsonStr param)
        {
            OptionModel info = new OptionModel();
            dataToOption(param, info);
            OptionTask task = new OptionTask();
            if (!await task.Add(info)) return Failed("0107", "存储失败");
            if (!await task.SetOptValues(info.OptName, info.OptValues)) return Failed("0107", "存储失败");
            return ExecuteResult.Success(info);
        }
        [Action("b2e046dd-92e5-4c7b-b922-6e2db86daa89", "系统选项", "修改系统选项信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> EditOption(JsonStr param)
        {
            if (param.Json["OptName"] == null)
                return Failed("0105", "执行参数异常，无法执行");
            OptionTask task = new OptionTask();
            OptionModel info = await task.GetByOPTION<OptionModel>(param.Json["OptName"].ToString());
            dataToOption(param, info);
            if (!await task.Edit(info)) return Failed("0107", "存储失败");
            if (!await task.SetOptValues(info.OptName, info.OptValues)) return Failed("0107", "存储失败");
            return ExecuteResult.Success(info);
        }
        [Action("3baf58ec-ee40-4661-a12a-4ab39ccbea58", "系统选项", "删除系统选项信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteOption(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            OptionTask task = new OptionTask();
            if (await task.Del(param.Json["ID"].ToString())) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        #endregion
        #region "ServiceManage"
        [Action("e48455e6-de33-4cc6-880d-2d8899efe37e", "系统服务", "系统服务管理界面", EControlKind.CK_View)]
        [Authorize]
        public async Task<IActionResult> ServiceManage()
        {
            return View(Runtime.ListService());
        }
        /// <summary>
        /// 根据主键 PK_Service 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("673e878a-f6c7-4c92-8bbd-6a5ce0738c95", "系统服务", "获取系统服务信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetByService(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(param.Json["ID"].ToString());
            WebServiceBase info = Runtime.GetService(ID);
            return ExecuteResult.Success(info);
        }
        [Action("44e4ec14-c10a-49cb-ba0a-3c009ce84265", "系统服务", "保存系统服务信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveService(JsonStr param)
        {
            WebServiceBase info = Runtime.GetService(Guid.Parse(param.Json["ID"].ToString()));
            if (info == null) return Failed("0003", "参数错误");
            info.ServiceName = param.Json["ServiceName"].ToString();
            info.InterKind = (ServiceInterKind)param.Json.Value<int>("InterKind");
            info.Intervalue = param.Json.Value<int>("Intervalue");
            info.Necessary = param.Json.Value<bool>("Necessary");
            info.Status = param.Json["Status"].ToString();
            info.Param = param.Json["Param"].ToString();
            Runtime.SaveService(info);
            return ExecuteResult.Success(info);
        }
        #endregion
        #region "SliderManage"
        [Action("944ad894-aba0-415e-a048-cfbdad2872eb", "主页广告", "主页广告管理", EControlKind.CK_View)]
        [Authorize]
        public async Task<IActionResult> SliderManage()
        {
            SliderTask task = new SliderTask();
            List<SliderInfo> list = await task.List<SliderInfo>("");
            return View(list);
        }

        /// <summary>
        /// 根据主键 PK_SLIDER 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("6e3ae46d-eead-4ea0-b139-e32cdb173b93", "主页广告", "获取主页广告信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetBySLIDER(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            SliderTask task = new SliderTask();
            SliderInfo info = await task.GetBySLIDER(ID);
            return ExecuteResult.Success(info);
        }
        [Action("2683591f-9102-4d8d-9dce-ffd334465491", "主页广告", "保存主页广告信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveSlider(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "") return await AddSlider(param);
            else return await EditSlider(param);
        }
        private void dataToSlider(JsonStr param, SliderModel info)
        {
            info.Kind = param.Json["Kind"].ToString();
            info.Title = param.Json["Title"].ToString();
            info.Import = param.Json["Import"].ToString();
            info.Content = param.Json["Content"].ToString();
            info.Validate = DateTime.Parse(param.Json["Validate"].ToString());
            info.Linker = param.Json["Linker"].ToString();
            info.DisplayOrder = param.Json.Value<int>("DisplayOrder");
        }
        [Action("06db8e02-3767-4d40-b3bc-9cd0401880b7", "主页广告", "添加主页广告信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> AddSlider(JsonStr param)
        {
            SliderModel info = new SliderModel();
            info.ID = 0;
            dataToSlider(param, info);
            SliderTask task = new SliderTask();
            if (!await task.Add(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("d3561bc1-308e-465d-b8d5-89b841a6625b", "主页广告", "修改主页广告信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> EditSlider(JsonStr param)
        {
            if (param.Json["ID"] == null || string.IsNullOrEmpty(param.Json["ID"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            SliderTask task = new SliderTask();
            SliderModel info = await task.GetBySLIDER(param.Json.Value<int>("ID"));
            dataToSlider(param, info);
            if (!await task.Edit(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("b2b460c5-575b-4899-ab06-d6ded40a2cc7", "主页广告", "删除主页广告信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteSlider(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            SliderTask task = new SliderTask();
            if (await task.Del(ID)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        #endregion
        #region "HtmlCatalogManage"
        [Action("6953fe8b-b599-42e4-9c55-6062c8c97ab4", "文本文档分类", "文本文档分类", EControlKind.CK_View)]
        [Authorize]
        public IActionResult HtmlCatalogManage()
        {
            return View();
        }
        /// <summary>
        /// 获取文本文档分类信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("9895b685-b405-4156-9acc-620131938c94", "文本文档分类", "获取文本文档分类信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetByHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlCatalogTask task = new HtmlCatalogTask();
            HtmlCatalogInfo info = await task.GetByHtmlCatalog<HtmlCatalogInfo>(ID);
            return ExecuteResult.Success(info);
        }
        private async Task<List<HtmlCatalogTree>> loadChildrenHtmlCatalog(HtmlCatalogTask task, int ID, bool hasChildren)
        {
            List<HtmlCatalogTree> list = await task.ListByPID<HtmlCatalogTree>(ID);  //原有功能
            if (hasChildren)
                foreach (HtmlCatalogTree m in list)
                    m.children = await loadChildrenHtmlCatalog(task, m.ID, true);
            return list;
        }
        /// <summary>
        /// 根据主键 PK_HtmlCatalog 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("{EACD99CF-3656-4D06-9270-D30DBB62F344}", "文本文档分类", "获取分类树", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> TreeOfHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlCatalogTask task = new HtmlCatalogTask();
            List<HtmlCatalogTree> tree = await loadChildrenHtmlCatalog(task, ID, true);
            return ExecuteResult.Success(tree);
        }
        [Action("f1ec5dd9-84ec-4844-aa28-3176e0e1e173", "文本文档分类", "移动文本文档分类位置", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> MoveHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null || string.IsNullOrEmpty(param.Json["ID"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            if (param.Json["DisplayOrder"] == null || string.IsNullOrEmpty(param.Json["DisplayOrder"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            HtmlCatalogTask task = new HtmlCatalogTask();
            return ExecuteResult.Success(await task.Move(param.Json.Value<int>("ID"), param.Json.Value<int>("DisplayOrder")));
        }
        [Action("6f60dacd-7cf0-46b8-8b3f-16c636f028da", "文本文档分类", "保存文本文档分类信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "") return await AddHtmlCatalog(param);
            else return await EditHtmlCatalog(param);
        }
        private void dataToHtmlCatalog(JsonStr param, HtmlCatalogInfo info)
        {
            //info.Icon = param.Json["Icon"].ToString();
        }
        [Action("8c8ef56f-c196-4035-bfbf-fffc707cb460", "文本文档分类", "添加文本文档分类信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> AddHtmlCatalog(JsonStr param)
        {
            HtmlCatalogTree info = new HtmlCatalogTree();
            info.ID = 0;
            info.CatalogName = param.Json["Catalog"].ToString();
            info.PID = param.Json.Value<int>("PID");
            info.Icon = "";
            info.DisplayOrder = 0;
            HtmlCatalogTask task = new HtmlCatalogTask();
            if (!await task.Add(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("b721ea99-0cc9-4e29-88d2-0406880265c2", "文本文档分类", "修改文本文档分类信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> EditHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null || string.IsNullOrEmpty(param.Json["ID"].ToString()))
                return Failed("0105", "执行参数异常，无法执行");
            HtmlCatalogTask task = new HtmlCatalogTask();
            HtmlCatalogInfo info = await task.GetByHtmlCatalog<HtmlCatalogInfo>(param.Json.Value<int>("ID"));
            info.CatalogName = param.Json["Catalog"].ToString();
            if (!await task.Edit(info))
                return Failed("0107", "存储失败");
            else return ExecuteResult.Success(info);
        }
        [Action("1ee1cd29-2ce3-4b95-87b6-297e751460b6", "文本文档分类", "删除文本文档分类信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteHtmlCatalog(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlCatalogTask task = new HtmlCatalogTask();
            if (await task.Del(ID)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        [Action("{3B67DC0D-EE4F-49F2-AC08-0F97EE51D310}", "文本文档分类", "获取分类文件列表", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> ListHtmlContent(JsonStr param)
        {
            if (param.Json["CatalogIdx"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("CatalogIdx");
            HtmlContentTask task = new HtmlContentTask();
            List<HtmlContentInfo> list = await task.ListByCONTENT_REF_HTMLCATALOG<HtmlContentInfo>(ID);
            return ExecuteResult.Success(list);
        }
        /// <summary>
        /// 获取文本文档内容信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("{42F06096-7C4A-4C23-B5AC-C30597D9F306}", "文本文档分类", "获取文本文档内容信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetByHtmlContent(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlContentTask task = new HtmlContentTask();
            HtmlContentModel info = await task.GetByHtmlContent<HtmlContentModel>(ID);
            info.Summary = await task.GetSummary(ID);
            info.Content = await task.GetContent(ID);
            return ExecuteResult.Success(info);
        }
        /// <summary>
        /// 保存文本文档内容信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("{DDF23D52-13C5-4199-8EC8-F88910826DDA}", "文本文档分类", "保存文本文档内容信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveHtmlContent(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            HtmlContentTask task = new HtmlContentTask();
            HtmlContentInfo info = (param.Json["ID"] == null || string.IsNullOrEmpty(param.Json["ID"].ToString())) ? new HtmlContentInfo() { ID = 0, Status = "编辑", Publishedate = Von.Library.Consts.MinDBDate } : await task.GetByHtmlContent<HtmlContentInfo>(param.Json.Value<int>("ID"));
            info.HtmlName = param.Json["HtmlName"].ToString();
            info.Title = param.Json["Title"].ToString();
            info.Autor = param.Json["Autor"].ToString();
            info.Src = param.Json["Src"].ToString();
            info.Keyword = param.Json["Keyword"].ToString();
            info.Tags = param.Json["Tags"].ToString();
            info.Linker = param.Json["Linker"].ToString();
            info.CatalogIdx = param.Json.Value<int>("CatalogIdx");
            info.Status = "编辑";
            info.BrowserCount = 0;
            info.Publishedate = Von.Library.Consts.MinDBDate;
            if (info.ID == 0)
            {
                info.Status = "编辑";
                if (!await task.Add(info)) return Failed("0107", "删除失败！");
            }
            else if (!await task.Edit(info)) return Failed("0107", "删除失败！");
            await task.SetSummary(info.ID, param.Json["Summary"].ToString());
            await task.SetContent(info.ID, param.Json["Content"].ToString());
            return ExecuteResult.Success(info);
        }
        [Action("{B0790C7B-EB66-4373-9E86-C09801131A4D}", "文本文档分类", "发布文本文档内容信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> PublishHtmlContent(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlContentTask task = new HtmlContentTask();
            if (await task.Publish(ID)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        [Action("{982B5D8D-F08C-4527-A263-880B2F0DA5E8}", "文本文档分类", "删除文本文档内容信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteHtmlContent(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            HtmlContentTask task = new HtmlContentTask();
            if (await task.Del(ID)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        #endregion
        #region "Comment"
        [Action("{357861AA-04B6-4E51-B24A-11676FAF9F03}", "评论回复", "评论回复管理", EControlKind.CK_View)]
        [Authorize]
        public async Task<IActionResult> CommentManage(int ID)
        {
            CommentTask task = new CommentTask();
            CommentInfo info = await task.GetByComment<CommentInfo>(ID);
            ViewBag.CurrentIdx = ID;
            if (info == null) return StatusView(EViewStatusClass.warning, "该评论已经被删除，或不存在。", new List<CommentInfo>());
            List<CommentInfo> list = await task.ListByComment<CommentInfo>(info.Kind, info.LinkIdx);
            return View(list);
        }
        /// <summary>
        /// 获取评价信息
        /// </summary>
        [Action("{25A7A6D7-985F-4FDE-ACA7-331E5ADEB67C}", "评论回复", "获取评价信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetComment(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            CommentTask task = new CommentTask();
            return ExecuteResult.Success(await task.GetByComment<CommentInfo>(ID));
        }
        /// <summary>
        /// 存储回复评价信息
        /// </summary>
        [Action("{2AA85F32-403C-41A0-BC55-A0A2264B39E2}", "评论回复", "回复评价信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> ReplyComment(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            CommentTask task = new CommentTask();
            CommentInfo info = await task.GetByComment<CommentInfo>(ID);
            if(await task.ReplyComment(ID, UserInfo.ID, param.Json["ReplyContent"].ToString()))
            {
                if (!string.IsNullOrEmpty(info.Email))
                {
                    string mailContent = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>系统回复</title>" +
                        "</head><body><style>.well {min-height: 20px; padding: 19px; margin-bottom: 20px; background-color: #f5f5f5;" +
                        "border: 1px solid #e3e3e3;border-radius: 4px;-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.05);box-shadow: inset 0 1px 1px rgba(0,0,0,.05);}</style>" +
                        "<p>感谢您对本网站的支持与关注，您的评论我们已经进行了回复。</p><p>您的评论如下：</p><div class=\"well\">" + info.Content +
                        "</div><p>我们给您的回复内容是：</p><div class=\"well\">" + param.Json["ReplyContent"].ToString() + "</div>" +
                        "<p>您也可以通过下面链接进行查看原文以及其他人的评论：</p><a href=\"" +
                        HttpContext.Request.Scheme + "://" + HttpContext.Request.Host.ToString() + info.LinkIdx + "\">查看原文</a></body></html>";
                    emailSender.SendMailAsync(info.Email, "评价回复", mailContent);
                }
                return ExecuteResult.Success(true);
            }
            return Failed("0007");
        }
        /// <summary>
        /// 删除评价信息
        /// </summary>
        [Action("{B8BE0E83-9663-4291-8136-2261CCCA1939}", "评论回复", "删除评价信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteComment(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            int ID = param.Json.Value<int>("ID");
            CommentTask task = new CommentTask();
            return ExecuteResult.Success(await task.Del(ID));
        }
        #endregion
        #region "ImageManage"
        [Action("b76b64c6-8e0a-40b3-b39a-0dcd9fb5f4f2", "图片资源管理", "图片资源管理", EControlKind.CK_View)]
        [Authorize]
        public async Task<IActionResult> ImageManage()
        {
            ImageTask task = new ImageTask();
            List<ImageInfo> list = await task.List<ImageInfo>();
            return View(list);
        }

        /// <summary>
        /// 根据主键 PK_Image 提取信息
        /// </summary>
        /// <param name="ID">序号</param>
        [Action("395092b1-9db3-4bde-a11d-9bccf042cc8d", "图片资源管理", "获取照片资源信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> GetByImage(JsonStr param)
        {
            ImageTask task = new ImageTask();
            if (param.Json["ID"] == null)
            {
                if (param.Json["Kind"] == null)
                    return Failed("0003", "参数错误系统拒绝执行！");
                if (param.Json["Code"] == null)
                    return Failed("0003", "参数错误系统拒绝执行！");
                string Kind = param.Json["Kind"].ToString();
                string Code = param.Json["Code"].ToString();
                return ExecuteResult.Success(await task.GetByImage<ImageInfo>(Kind, Code));
            }
            Guid ID = Guid.Parse(param.Json["ID"].ToString());
            return ExecuteResult.Success(await task.GetByImage<ImageInfo>(ID));
        }
        [Action("9af4fa91-2953-4061-b189-bef3a408d9fc", "图片资源管理", "保存照片资源信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> SaveImage(JsonStr param)
        {
            if (param.Json["ID"] == null || param.Json["ID"].ToString() == "") return await AddImage(param);
            else return await EditImage(param);
        }
        private async Task<ExecuteResult> SaveImageFormData(IFormCollection param, bool isNew)
        {
            if (param.Files.Count == 0) return Failed("0003", "未检测到文件！");
            ImageInfo info = new ImageInfo();
            info.Kind = param["Kind"].ToString();
            info.Code = param["Code"].ToString();
            info.ImageName = param["ImageName"].ToString();
            var file = param.Files[0];
            info.Ext = file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
            ImageTask task = new ImageTask();
            if (isNew)
            {
                info.ID = Guid.NewGuid();
                if (!await task.Add(info))
                    return Failed("0107", "存储失败");
            }
            else
            {
                info.ID = Guid.Parse(param["ID"].ToString());
                if (!await task.Edit(info))
                    return Failed("0107", "存储失败");
            }
            using (MemoryStream fs = new MemoryStream())
            {
                file.CopyTo(fs);
                fs.Position = 0;
                await task.SetImg(info.ID, fs);
                fs.Close();
            }
            return ExecuteResult.Success(info);
        }
        [Action("ae17cf11-0804-4c26-9156-1ee4f289ebd6", "图片资源管理", "添加照片资源信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> AddImage(JsonStr param)
        {
            return await SaveImageFormData(Request.Form, true);
        }
        [Action("bc9ff5bb-499c-470b-9abd-c2240f2f11d8", "图片资源管理", "修改照片资源信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> EditImage(JsonStr param)
        {
            return await SaveImageFormData(Request.Form, false);
        }
        [Action("7191ba9b-6022-4efb-9af3-d37ed45c091c", "图片资源管理", "删除照片资源信息", EControlKind.CK_Ajax)]
        [Authorize]
        public async Task<ExecuteResult> DeleteImage(JsonStr param)
        {
            if (param.Json["ID"] == null)
                return Failed("0003", "参数错误系统拒绝执行！");
            Guid ID = Guid.Parse(param.Json["ID"].ToString());
            ImageTask task = new ImageTask();
            if (await task.Del(ID)) return ExecuteResult.Success(true);
            return Failed("0107", "删除失败！");
        }
        #endregion
    }
}
